package model;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import com.sun.org.apache.regexp.internal.recompile;

public class Message {
	int mid, uid;
	String m_name, m_content, m_title;

	public Message(int mid, int uid, String m_name, String m_title,
			String m_content) {
		this.mid = mid;
		this.uid = uid;
		this.m_title = m_title;
		this.m_name = m_name;
		this.m_content = m_content;
	}

	public int getMid() {
		return mid;
	}

	public void setMid(int mid) {
		this.mid = mid;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public void setM_title(String m_title) {
		this.m_title = m_title;
	}

	public String getM_title() {
		return m_title;
	}

	public String getM_name() {
		return m_name;
	}

	public void setM_name(String m_name) {
		this.m_name = m_name;
	}

	public String getM_content() {
		return m_content;
	}

	public void setM_content(String m_content) {
		this.m_content = m_content;
	}

	public Message() {
	}

	public String checkMGName(String n) {
		String name = this.m_name;
		if (name == null) {
			return "Bạn chưa nhập Message Name.";
		} else {
			name = name.trim();
			if (name.equals("")) {
				return "Bạn chưa nhập Message Name .";
			} else {
				if (!n.equals("n")) {
					if (this.getMgByName(name) != null) {
						return "Message này đã tồn tại.";
					}
				}
			}
		}
		return "";
	}

	public String checkMGT() {
		String name = this.m_title;
		if (name == null) {
			return "Bạn chưa nhập Message Title.";
		} else {
			name = name.trim();
			if (name.equals("")) {
				return "Bạn chưa nhập Message Title.";
			}
		}
		return "";
	}

	public String checkMG() {
		String name = this.m_content;
		if (name == null) {
			return "Bạn chưa nhập Message.";
		} else {
			name = name.trim();
			if (name.equals("")) {
				return "Bạn chưa nhập Message.";
			}
		}
		return "";
	}

	public boolean Create() {
		PreparedStatement sqlr = null;
		Connection con = new ConnectDB().getConnection();
		try {
			sqlr = con
					.prepareStatement("INSERT INTO message(uid , m_name, m_title, m_content) VALUES (?, ?, ?, ?)");
			sqlr.setInt(1, this.uid);
			sqlr.setString(2, this.m_name);
			sqlr.setString(3, this.m_title);
			sqlr.setString(4, this.m_content);
			int number = sqlr.executeUpdate();
			if (number > 0) {
				return true;
			}
			sqlr.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public Message getMgByName(String mg_name) {
		Message message = null;
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection
					.prepareStatement("SELECT * FROM message WHERE m_name = ?");
			sqlp.setString(1, mg_name);
			ResultSet rs = sqlp.executeQuery();
			while (rs.next() == true) {
				int mid = rs.getInt(1);
				int uid = rs.getInt(2);
				String m_n = rs.getString(3);
				String m_t = rs.getString(4);
				String m_ct = rs.getString(5);
				message = new Message(mid, uid, m_n, m_t, m_ct);
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return message;
	}

	public Message getMg(int mid) {
		Message message = null;
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection
					.prepareStatement("SELECT * FROM message WHERE mid = ?");
			sqlp.setInt(1, mid);
			ResultSet rs = sqlp.executeQuery();
			while (rs.next() == true) {
				int mid1 = rs.getInt(1);
				int uid = rs.getInt(2);
				String m_n = rs.getString(3);
				String m_t = rs.getString(4);
				String m_ct = rs.getString(5);
				message = new Message(mid1, uid, m_n, m_t, m_ct);
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return message;
	}

	public int getSizeDB() {
		int result = 0;
		PreparedStatement sqlu = null;
		Connection con = new ConnectDB().getConnection();
		try {
			sqlu = con.prepareStatement("select COUNT(mid) from message");
			ResultSet rs = sqlu.executeQuery();
			while (rs.next()) {
				return rs.getInt(1);
			}
			sqlu.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public int[] getMID(String orby /* n_name */) {
		if (orby == null) {
			orby = "m_name";
		} else {
			orby = orby.trim();
			if (!((orby.equals("m_name")) || (orby.equals("m_title"))
					|| (orby.equals("uid")) || (orby.equals("m_content")) || (orby
						.equals("")))) {
				orby = "position";
			}
		}
		int[] result = new int[this.getSizeDB()];
		int i = 0;
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection
					.prepareStatement("SELECT mid FROM message ORDER BY "
							+ orby + " ASC  ");
			ResultSet rs = sqlp.executeQuery();
			while (rs.next() == true) {
				result[i] = rs.getInt(1);
				i++;
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public String toString() {
		return this.mid + " " + this.uid + " " + this.m_name + " "
				+ this.m_title + " " + this.m_content;
	}

	

	public boolean isMID(String mid) throws IOException{ /*kiem tra tinh hop le cua nid va co nam trong DB hay khong*/
		if(mid==null) return false;
		if(mid.trim().equals(""))return false;
		mid.trim();
		int id = -1;
		try {
			id = Integer.parseInt(mid);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT mid FROM message where mid= ? ");
			sqlp.setInt(1, id);
			ResultSet rs = sqlp.executeQuery();
			while (rs.next() == true) {
				return true;
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}	

	public boolean del(int id) {
		PreparedStatement delpd = null;
		Connection con = new ConnectDB().getConnection();
		try {
			delpd = con.prepareStatement("DELETE FROM message WHERE mid = ?");
			delpd.setInt(1, id);
			int number = delpd.executeUpdate();
			if (number > 0) {
				return true;
			}
			delpd.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public String reDirect(String a) {
		SetUp setUp= new SetUp();
		HashMap<String, String> link = new HashMap<String, String>();
		link.put("home",setUp.BASE_URL+"/index.jsp");
		link.put("a", setUp.BASE_URL+"/a/a.jsp");
		link.put("vn", setUp.BASE_URL+"/a/national.jsp");
		link.put("vm", setUp.BASE_URL+"/a/messages.jsp");
		link.put("vu", setUp.BASE_URL+"/a/user.jsp");
		link.put("vc", setUp.BASE_URL+"/a/categories.jsp");
		link.put("vac", setUp.BASE_URL+"/a/actor.jsp");
		link.put("in", setUp.BASE_URL+"/index.jsp");
		
		link.put("errnotupdate", setUp.BASE_URL+"/a/message.jsp?m=erroo&r=home");
		if (a == null) {
			return link.get("home");
		} else {
			if (link.get(a) == null) {
				return link.get("errnotupdate");
			} else {
				return link.get(a);
			}
		}
	}

	public boolean upDate() {
		PreparedStatement sqlr = null;
		boolean result = false;

		Connection con = new ConnectDB().getConnection();
		try {
			sqlr = con
					.prepareStatement("UPDATE message SET uid = ?,  m_name = ?, m_title = ? , m_content = ? WHERE mid = ? LIMIT 1");
			sqlr.setInt(1, this.getUid());
			sqlr.setString(2, this.getM_name());
			sqlr.setString(3, this.getM_title());
			sqlr.setString(4, this.getM_content());
			sqlr.setInt(5, this.getMid());
			int number = sqlr.executeUpdate();
			if (number > 0) {
				result = true;
			}
			sqlr.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
}
